package com.youlai.boot.factory.controller;

import com.youlai.boot.factory.model.vo.GarmentStyleVO;
import com.youlai.boot.factory.model.vo.ProductionProgressVO;
import com.youlai.boot.factory.service.GarmentCuttingBedService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.GarmentCuttingBedForm;
import com.youlai.boot.factory.model.query.GarmentCuttingBedQuery;
import com.youlai.boot.factory.model.vo.GarmentCuttingBedVO;

import java.util.List;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 裁床单前端控制层
 *
 * @author youlaitech
 * @since 2025-08-24 14:08
 */
@Tag(name = "裁床单接口")
@RestController
@RequestMapping("/api/v1/garment-cutting-bed")
@RequiredArgsConstructor
public class GarmentCuttingBedController  {

    private final GarmentCuttingBedService garmentCuttingBedService;

    @Operation(summary = "裁床单分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:garment-cutting-bed:query')")
    public PageResult<GarmentCuttingBedVO> getGarmentCuttingBedPage(GarmentCuttingBedQuery queryParams ) {
        IPage<GarmentCuttingBedVO> result = garmentCuttingBedService.getGarmentCuttingBedPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增裁床单")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:garment-cutting-bed:add')")
    public Result<Void> saveGarmentCuttingBed(@RequestBody @Valid GarmentCuttingBedForm formData ) {
        boolean result = garmentCuttingBedService.saveGarmentCuttingBed(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取裁床单表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:garment-cutting-bed:edit')")
    public Result<GarmentCuttingBedForm> getGarmentCuttingBedForm(
        @Parameter(description = "裁床单ID") @PathVariable Long id
    ) {
        GarmentCuttingBedForm formData = garmentCuttingBedService.getGarmentCuttingBedFormData(id);
        return Result.success(formData);
    }


    @Operation(summary = "获取裁床单连表表单数据")
    @GetMapping("/{id}/joinForm")
    @PreAuthorize("@ss.hasPerm('factory:garment-cutting-bed:edit')")
    public Result<GarmentCuttingBedVO> getGarmentStyleFormWithCustomer(
            @Parameter(description = "裁床单ID") @PathVariable Long id
    ) {
        GarmentCuttingBedVO formData = garmentCuttingBedService.getGarmentCuttingBedJoinFormData(id);
        return Result.success(formData);
    }


    @Operation(summary = "修改裁床单")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:garment-cutting-bed:edit')")
    public Result<Void> updateGarmentCuttingBed(
            @Parameter(description = "裁床单ID") @PathVariable Long id,
            @RequestBody @Validated GarmentCuttingBedForm formData
    ) {
        boolean result = garmentCuttingBedService.updateGarmentCuttingBed(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除裁床单")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:garment-cutting-bed:delete')")
    public Result<Void> deleteGarmentCuttingBeds(
        @Parameter(description = "裁床单ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = garmentCuttingBedService.deleteGarmentCuttingBeds(ids);
        return Result.judge(result);
    }

    /**生成裁床单图片*/
    @Operation(summary = "生成裁床单图片")
    @GetMapping("/{id}/generateImage")
    public Result<String> generateImage(
            @Parameter(description = "裁床明细ID") @PathVariable Long id
    ) throws IOException {
        String base64Image = garmentCuttingBedService.generateImage(id);
        return Result.success(base64Image);
    }

    @Operation(summary = "获取裁床单生产进度")
    @GetMapping("/{id}/productionProgress")
    @PreAuthorize("@ss.hasPerm('factory:garment-cutting-bed:query')")
    public Result<List<ProductionProgressVO>> getProductionProgress(
            @Parameter(description = "裁床单ID") @PathVariable Long id
    ) {
        List<ProductionProgressVO> progressList = garmentCuttingBedService.getProductionProgress(id);
        return Result.success(progressList);
    }
}
